﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using YiSha.Data.Repository;
using YiSha.Entity;
using YiSha.Enums;
using YiSha.SqlSugars;
using YiSha.Util;
using YiSha.Util.Model;

namespace YiSha.Service
{
    /// <summary>
    /// 图片服务
    /// </summary>
    public class ImaService : RepositoryFactory
    {
        /// <summary>
        /// 获取图片信息
        /// </summary>
        /// <param name="id">以逗号分割</param>
        /// <returns></returns>
        public List<SysImageInfo> GetList(List<string> id)
        {
            try
            {
                var tempList = id.Where(w => w.Contains(",")).ToList();
                if (tempList.Count > 0)
                {
                    var strId = new List<string>();
                    foreach (var item in id)
                    {
                        item.Split(",").ToList().ForEach(f =>
                        {
                            strId.Add(f);
                        });
                    }
                    var result = new List<SysImageInfo>();
                    // var sql = "SELECT Id,ImagePath FROM `SysImageInfo` where  Id in(@Id)";
                    var imgList = SqlSugarAdapters.GetList<SysImageInfo>(s => strId.Contains(s.Id));
                    id.ForEach(f =>
                    {
                        var temp = f.Split(",").ToList();
                        var imgInfo = imgList.Where(w => temp.Contains(w.Id)).ToList();
                        result.Add(new SysImageInfo
                        {
                            Id = string.Join(",", imgInfo.Select(s => s.Id)),
                            ImagePath = string.Join(",", imgInfo.Select(s => s.ImagePath)),
                        });
                    });
                    return result;
                }
                else
                {
                    return SqlSugarAdapters.GetList<SysImageInfo>(s => id.Contains(s.Id));
                }
                throw new Exception("id不能为空");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获取图片信息
        /// </summary>
        /// <param name="id">以逗号分割</param>
        /// <returns></returns>
        public List<SysImageInfo> GetList(string id)
        {
            try
            {
                if (!id.IsEmpty() && id.Contains(","))
                {
                    var arr = id.Split(",").ToList();
                    return SqlSugarAdapters.GetList<SysImageInfo>(s => arr.Contains(s.Id));
                }
                throw new Exception("id为空或者格式不正确，请检查参数");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获取图片信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public SysImageInfo GetEntity(string id)
        {
            try
            {
                return SqlSugarAdapters.GetEntity<SysImageInfo>(s => s.Id == id);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public TData Delete(string id)
        {
            TData result = new TData { MsgCode = MsgCode.Fail };
            try
            {
                if (id.IsEmpty())
                {
                    result.Msg = "id不能为空";
                    return result;
                }
                var imgInfo = SqlSugarAdapters.GetEntity<SysImageInfo>(s => s.Id == id);
                if (!imgInfo.IsEmpty())
                {
                    string absoluteDir = Path.Combine(GlobalContext.HostingEnvironment.ContentRootPath, imgInfo.ImagePath);
                    //如果文件已经存在
                    if (System.IO.File.Exists(absoluteDir) == true)
                    {
                        //删除文件
                        System.IO.File.Delete(absoluteDir);
                        SqlSugarAdapters.Delete<SysImageInfo>(s => s.Id == id);
                        //
                        result.MsgCode = MsgCode.Success;
                        result.Msg = "操作成功";
                        return result;
                    }
                }
                result.Msg = "操作失败，请稍后再试";
                return result;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
                return result;
            }
        }
        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public TData Delete(List<string> id)
        {
            TData result = new TData { MsgCode = MsgCode.Fail };
            //使用事务添加数据
            using (SqlSugarClient db = SugarBase.GetInstance()) //开启数据连接
            {
                try
                {
                    //开始事务
                    db.BeginTran();
                    //查询图片信息
                    var imgInfo = db.Queryable<SysImageInfo>().Where(s => id.Contains(s.Id)).ToList();
                    imgInfo.ForEach(f =>
                    {
                        string absoluteDir = Path.Combine(GlobalContext.HostingEnvironment.ContentRootPath, f.ImagePath);
                        //如果文件已经存在
                        if (System.IO.File.Exists(absoluteDir) == true)
                        {
                            //删除文件
                            System.IO.File.Delete(absoluteDir);
                            //删除图片信息
                            db.Deleteable<SysImageInfo>(s => id.Contains(s.Id)).ExecuteCommand();
                        }
                    });
                    //提交事务
                    db.CommitTran();
                    //
                    result.MsgCode = MsgCode.Success;
                    result.Msg = "操作成功";
                }
                catch (Exception ex)
                {
                    //回滚
                    db.RollbackTran();
                    //
                    result.Msg = ex.Message;
                }
            }
            return result;
        }
    }
}
